package io.coderyeah.org.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.coderyeah.basic.Result;
import io.coderyeah.org.domain.OperationLog;
import io.coderyeah.org.dto.OperationLogDTO;
import io.coderyeah.org.mapper.OperationLogMapper;
import io.coderyeah.org.service.OperationLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service
@Transactional
public class OperationLogServiceImpl extends ServiceImpl<OperationLogMapper, OperationLog> implements OperationLogService {
    @Autowired
    private OperationLogMapper logMapper;

    @Override
    public Result saveOrUpdate(OperationLogDTO logDTO) {
        if (null != logDTO.getId()) {
            // 修改
            final OperationLog operationLog = BeanUtil.copyProperties(logDTO, OperationLog.class);
            operationLog.setUpdateTime(new Date());
            logMapper.updateById(operationLog);
            return Result.success("修改成功");
        } else {
            // 添加
            final OperationLog operationLog = BeanUtil.copyProperties(logDTO, OperationLog.class);
            operationLog.setCreateTime(new Date());
            logMapper.insert(operationLog);
            return Result.success("添加成功");
        }
    }
}
